package com.example.bot_cc.widgettest_404lcw;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv;
    private EditText et;
    private Button btngettext, btnsetimg,getGender,getHobby;
    private RadioButton boy,girl;
    private CheckBox basketball,football,volleyball;
    private ImageView iv;
    private ImageButton ibtn;
    private RadioGroup rg;
    private  String hobby="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init()
    {
        //获取控件
        rg = (RadioGroup)findViewById(R.id.radioGroup);
        tv = (TextView)findViewById(R.id.textView);
        et = (EditText)findViewById(R.id.editText);
        btngettext = (Button)findViewById(R.id.setText);
        btnsetimg = (Button)findViewById(R.id.setImg);
        iv = (ImageView)findViewById(R.id.imageView);
        ibtn = (ImageButton)findViewById(R.id.imageButton);
        getGender = (Button)findViewById(R.id.get_gender);
        getHobby = (Button)findViewById(R.id.get_hobby);
        boy = (RadioButton)findViewById(R.id.boy);
        girl = (RadioButton)findViewById(R.id.girl);
        basketball = (CheckBox)findViewById(R.id.basketball);
        football = (CheckBox)findViewById(R.id.football);
        volleyball = (CheckBox)findViewById(R.id.vollyball);
        //设置按钮监听器
        btngettext.setOnClickListener(this);
        btnsetimg.setOnClickListener(this);
        ibtn.setOnClickListener(this);
        getGender.setOnClickListener(this);
        getHobby.setOnClickListener(this);

        volleyball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                    hobby += "排球+";
                else
                    hobby = hobby.replace("排球+","");
                tv.setText(hobby);
            }
        });

        football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                    hobby += "足球+";
                else
                    hobby = hobby.replace("足球+","");
                tv.setText(hobby);
            }
        });

        basketball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                    hobby += "篮球+";
                else
                    hobby = hobby.replace("篮球+","");
                tv.setText(hobby);
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i)
                {
                    case R.id.boy:
                        tv.setText("男");
                        break;
                    case R.id.girl:
                        tv.setText("女");
                        break;
                }
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.setText: //设置文本
                String str = et.getText().toString();
                tv.setText(str);
                break;
            case R.id.setImg: //设置图片
                iv.setImageResource(R.drawable.f_huh);
                break;
            case R.id.imageButton: //图片按钮
                Intent intent = new Intent(MainActivity.this, newActivity.class);
                intent.putExtra("param1",et.getText().toString());
                intent.putExtra("param2",2);
                startActivity(intent);
                break;
            case R.id.get_gender:
                if(boy.isChecked()) tv.setText("男");
                if(girl.isChecked()) tv.setText("女");
                break;
            case R.id.get_hobby:
                String msg="";
                if(basketball.isChecked()) msg+="篮球";
                if(football.isChecked()) msg+="足球";
                if(volleyball.isChecked()) msg+="排球";
                tv.setText(msg);
                break;
        }
    }
}
